package com.ipharma;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ipharma.application.services.transaccion.Ticket;
import com.ipharma.assignments.Assignment;
import com.ipharma.config.Config;
import com.ipharma.config.Session;
import com.ipharma.gui.GUIMain;
import com.ipharma.gui.GUIMenu;

/**
 * En esta clase esta el main() que arranca la aplicacion.
 * 
 * @author afortunato
 */
public class McdMain{

   /**
    * Attributes
    */
   public Session session;

   /*
    * public Identifier identity; DBManager dbm;
    */
   JFrame app = new JFrame("MCD");

   GUIMain guiMain = null;

   GUIMenu guiMenu = null;

   /**
    * Instancia para logging.
    */
   private static final Log log = LogFactory.getLog(McdMain.class);
   
   Assignment assign; 
   
   Ticket ticket;
    

   /**
    * Constructor
    */
   public McdMain() {

   }

   /**
    * Initializes application parameters
    * 
    * @param args
    */
   public void init(String[] args) {

      try {
         // reads application parameters from XML file
         String xx = System.getProperty( "user.dir" );
         log.info("user.dir: " + xx);
         Config cfg = new Config(xx + McdPrs.dirPrivate + McdPrs.fileParameters);
         this.session = new Session();
         session.put(McdPrs.CONFIG, cfg);
         /*
          * session.put( McdPrs.projectHome, System.getProperty( "user.dir" ) );
          * cfg = new Config( session.getString( McdPrs.projectHome ) +
          * McdPrs.fileConnect ); session.put( McdPrs.dbUrl, cfg.getDburl() );
          * session.put( McdPrs.dbUser, cfg.getUser() ); session.put(
          * McdPrs.dbPass, cfg.getPasswd() ); session.put( McdPrs.dispenserId,
          * cfg.getDispenser() );
          * 
          * this.connect(); session.put( McdPrs.aConnection, dbm.getConnection() );
          l_param = new Parameter( session ); // loads db parameters
          session.put( McdPrs.operMode, l_param.getValue( McdPrs.operMode ) );
          session.put( McdPrs.assignMode, l_param.getValue( McdPrs.assignMode ) );
          session.put( McdPrs.ticketGen, l_param.getValue( McdPrs.ticketGen ) );
          session.put( McdPrs.tktWidth, l_param.getValue( McdPrs.tktWidth ) );
          session.put( McdPrs.tktPort, l_param.getValue( McdPrs.tktPort ) );
          session.put( McdPrs.tktTitle, l_param.getValue( McdPrs.tktTitle ) );
          session.put( McdPrs.timeout, l_param.getValue( McdPrs.timeout ) );
          session.put( McdPrs.userIdGen, l_param.getValue( McdPrs.userIdGen ) );
          session.put( McdPrs.checkPrints, l_param.getValue(
          * McdPrs.checkPrints ) ); session.put( McdPrs.siteName,
          * l_param.getValue( McdPrs.siteName ) ); session.put( McdPrs.maxProds,
          * l_param.getValue( McdPrs.maxProds ) );
          *  // puts application frame into session session.put(
          * McdPrs.appFrame, app.getContentPane() );
          */
         // sets properties
         app.setSize(McdPrs.appResolution);
         app.setLocation(0, -20);
         app.setResizable(false);
         app.addWindowListener(new Main_exit_windowAdapter(this));

      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Starts database connection
    * 
    * @param args
    */
   public void connect() {

      String ls_usr, ls_db;
      /*
       * dbm = new DBManager();
       * 
       * try { ls_usr = session.getString( McdPrs.dbUser ) + "/" +
       * session.getString( McdPrs.dbPass ); ls_db = session.getString(
       * McdPrs.dbUrl ); ls_db = ls_db.substring( 0, ls_db.indexOf( "@" ) ) +
       * ls_usr + ls_db.substring( ls_db.indexOf( "@" ) );
       * 
       * dbm.openConnection( ls_db ); } catch ( Exception e ) { McdError.add( e ); }
       */
   }

   public void setWindow(JPanel pPan) {

      app.getContentPane().removeAll();
      app.getContentPane().add(pPan);
      app.setVisible(true);
   }

   /**
    * Starts user interface
    */
   public void start() {

      try {
         // adds main panel to application window
         guiMain = new GUIMain(this);
         setWindow(guiMain);
         guiMain.start();

         // guiMain will call verify() when user identificates
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   

   /**
    * Shows assignment screen if required
    */
   public void menu() {

      try {
         // creates ticketer and puts it into session
         Config config = (Config) session.get(McdPrs.CONFIG);
         if ( config.getTicketGeneration().equals( McdPrs.D_YES ) ) {
            ticket = new Ticket( session );
            session.put( McdPrs.aTicketer, ticket ); 
         }
         
         guiMenu = new GUIMenu(this);
         setWindow(guiMenu);
         guiMenu.start();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Ends the application
    */
   public void exitWindowClosing() {

      System.out.println("Exiting!");
      System.exit(0);
   }

   /**
    * Sets apllication window title
    */
   public void setAppTitle(String pTit) {

      app.setTitle(pTit);
   }

   /**
    * Returns application frame
    * 
    * @return the application jframe
    */
   public JFrame getFrame() {

      return app;
   }

   /**
    * main
    * 
    * @param args
    */
   public static void main(String[] args) {

      McdMain mcdMain = new McdMain();

      try {
         // initializes parameters and connect to DB
         mcdMain.init(args);
         // starts GUI interface
         mcdMain.start();
      } catch (Exception e) {
         McdError.add(e);
      }
   }
}

class Main_exit_windowAdapter extends java.awt.event.WindowAdapter {

   McdMain adaptee;

   Main_exit_windowAdapter(McdMain adaptee) {

      this.adaptee = adaptee;
   }

   public void windowClosing(WindowEvent e) {

      adaptee.exitWindowClosing();
   }
}
